import { useState, useCallback } from 'react';
import { useTVEventHandler } from 'react-native';

export interface FocusableItem {
  id: string;
  onFocus?: () => void;
  onPress?: () => void;
}

export const useSimpleFocus = (items: FocusableItem[]) => {
  const [focusedIndex, setFocusedIndex] = useState(0);

  const focusItem = useCallback((index: number) => {
    if (index >= 0 && index < items.length) {
      setFocusedIndex(index);
      items[index].onFocus?.();
    }
  }, [items]);

  const handlePress = useCallback(() => {
    items[focusedIndex]?.onPress?.();
  }, [items, focusedIndex]);

  useTVEventHandler((evt: any) => {
    const { eventType } = evt;
    
    switch (eventType) {
      case 'left':
        focusItem(focusedIndex - 1);
        break;
      case 'right':
        focusItem(focusedIndex + 1);
        break;
      case 'select':
      case 'enter':
        handlePress();
        break;
    }
  });

  return {
    focusedIndex,
    focusItem,
    isFocused: (index: number) => focusedIndex === index,
  };
}; 